<?php
if (!defined('DCS_ROOT')) {
	exit('Access Denied');
} 

class MovieModel {
	var $DB;
	function __construct ($DB) {
		$this->DB = $DB;
	} 

	function getOne($id) {
		$sql = "SELECT * FROM movie WHERE id = '$id' ";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	}

	function getList($condition) {
		$sql = "SELECT * FROM movie ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} ";
		} 
		if (!empty($condition['page']) && !empty($condition['rows'])) {
			$start = ($condition['page'] - 1) * $condition['rows'];
			$tmp .= "LIMIT " . $start . ", " . $condition['rows'];
		} 
		$sql .= $tmp;
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}

	function total($condition) {
		$sql = "SELECT count(*) AS total FROM movie ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} ";
		} 
		$sql .= $tmp; 
		$this->DB->query($sql);
		$this->DB->fetchRow();
		return $this->DB->getValue("total");
	}
	
	function addOne($title,$playtime,$endtime,$story,$subdesc,$descs,$mtips,$thumb){
		global $timestamp;
		$sql = "INSERT INTO movie (title,playtime,endtime,addtime,story,subdesc,descs,mtips,thumb) VALUES ('$title','$playtime','$endtime','$timestamp','$story','$subdesc','$descs','$mtips','$thumb')";
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}	

	function editOne($id,$title,$playtime,$endtime,$story,$subdesc,$descs,$mtips,$thumb) {
		$sql = "UPDATE movie SET 		
		title='$title',playtime='$playtime',endtime='$endtime',thumb='$thumb',story='$story',subdesc='$subdesc',descs='$descs',mtips='$mtips'		
		WHERE id='$id'";
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}

	function delOne($id) {
		$sql = "DELETE FROM movie WHERE id = '$id'" ;
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	} 
} 

?>
